home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / GCC 1.37.1r14 / usr / gcc-1.37.1r14 / object oriented files / CDRVRArray.cp < prev    next >
Encoding:
Text File  |  1993-11-03  |  2.9 KB  |  122 lines  |  [TEXT/KAHL]

  1. #include <qsort++.h>
  2. #include "CDRVRArray.h"
  3.  
  4. #define    N_TXTOFF(ex)  sizeof(struct exec)
  5. #define    N_DATOFF(ex) (N_TXTOFF(ex) + (ex).a_text)
  6. #define N_SYMOFF(ex) (N_DATOFF(ex) + (ex).a_data + (ex).a_trsize + (ex).a_drsize)
  7. #define    N_STROFF(ex) (N_SYMOFF(ex) + (ex).a_syms)
  8.  
  9. void CDRVRArray::IResHandle( Handle itsData)
  10.       {
  11.       int i,nsyms;
  12.       struct nlist *sp;
  13.       char *strtbl;
  14.     
  15.       HLock(itsData);
  16.       execp = (void *)(244+*itsData);
  17.       
  18.       nsyms = execp->a_syms / sizeof (struct nlist);
  19.       
  20.       IArray(sizeof (struct nlist));
  21.           
  22.       Resize(nsyms);
  23.  
  24.       strtbl = (char *)execp+N_STROFF(*execp);            
  25.       sp = (void *)((char *)execp+N_SYMOFF (*execp));
  26.       
  27.       for (i = 1; i <= nsyms; i++)
  28.             {
  29.             struct nlist newlist = *sp;
  30.               if (newlist.n_un.n_strx == 0)
  31.                 newlist.n_un.n_name = "";
  32.               else if (newlist.n_un.n_strx < 0 /* || newlist.n_un.n_strx > strsize */)
  33.                 newlist.n_un.n_name = "<bad string table index>";
  34.               else
  35.                 newlist.n_un.n_name = strtbl + newlist.n_un.n_strx;
  36.             InsertAtIndex(&newlist, 1);        
  37.             sp++;
  38.             }
  39.       }
  40.  
  41. /******************************************************************************
  42.  Sort
  43. ******************************************************************************/
  44.  
  45. Boolean            CDRVRArray::cCompNumerically;    // compare for Numerically sort if true
  46. CDRVRArray        *CDRVRArray::cCurrArray;    // array being sorted
  47.  
  48. /* Comparison functions for sorting symbols.  */
  49.  
  50. #define reverse_sort 0
  51.  
  52. static int
  53. alphacompare (struct nlist *sym1, struct nlist *sym2)
  54. {
  55. int strcmp(char *,char *);
  56.   if (reverse_sort)
  57.     {
  58.       if (!sym2->n_un.n_name)
  59.     {
  60.       if (sym1->n_un.n_name) return -1;
  61.       else return 0;
  62.     }
  63.       if (!sym1->n_un.n_name) return 1;
  64.       return strcmp (sym2->n_un.n_name, sym1->n_un.n_name);
  65.     }
  66.   else
  67.     {
  68.       if (!sym1->n_un.n_name)
  69.     {
  70.       if (sym2->n_un.n_name) return -1;
  71.       else return 0;
  72.     }
  73.       if (!sym2->n_un.n_name) return 1;
  74.       return strcmp (sym1->n_un.n_name, sym2->n_un.n_name);
  75.     }
  76. }
  77.  
  78.  
  79. static int
  80. valuecompare (
  81.      struct nlist *sym1, struct nlist *sym2)
  82. {
  83.   if (reverse_sort)
  84.     return sym2->n_value - sym1->n_value;
  85.   else
  86.     return sym1->n_value - sym2->n_value;
  87. }
  88.  
  89. void CDRVRArray::swap( size_t i, size_t j)
  90.     {
  91.         CDRVRArray::cCurrArray->Swap( i+1, j+1);
  92.     }
  93.     
  94. int CDRVRArray::CompareStrings( size_t index1, size_t index2)
  95.     {
  96.         unsigned char *items = (unsigned char*) *CDRVRArray::cCurrArray->hItems;
  97.         
  98.         int (*func)(void *,void *) = (void *)(cCompNumerically ? valuecompare : alphacompare);
  99.         
  100.         return func( items + CDRVRArray::cCurrArray->ItemOffset( index1+1), 
  101.                         items + CDRVRArray::cCurrArray->ItemOffset( index2+1));
  102.     }
  103.  
  104. void CDRVRArray::Sort( Boolean sort_numerically)
  105. {
  106.     SignedByte    state = HGetState( hItems);
  107.     
  108.         /* qsort won't move memory, but LoadSeg can        */
  109.         
  110.     cCompNumerically = sort_numerically;
  111.     cCurrArray = this;
  112.     
  113.     HLock( hItems);
  114.     
  115.     qsortplusplus( numItems, CompareStrings, swap);    // TCL 1.1.3 MER 11/7/92, 3/15/93
  116.     
  117.     HSetState( hItems, state);
  118.     
  119.     cCurrArray = NULL;
  120.  
  121. }    /* CDRVRArray::Sort */
  122.